package Offer2017;

/**
 * 树的子结构
 * Created by 雷文康 on 2017/12/31.
 */
public class Test18 {


    public static boolean isSubTree(TreeNode root1, TreeNode root2){
        if (root1 == null) {
            return false;
        }
        if (root2 == null){
            return true;
        }
        boolean result = false;
        if (root1.value == root2.value){
            result = doesTree1HasTree2(root1, root2);
        }
        if (!result) {
            result = isSubTree(root1.left, root2);
        }
        if (!result) {
            result = isSubTree(root1.right, root2);
        }
        return result;

    }

    private static boolean doesTree1HasTree2(TreeNode root1, TreeNode root2){
        if (root1 == null){
            return false;
        }
        if (root2 == null){
            return  true;
        }
        if (root1.value != root2.value){
            return false;
        }
        return doesTree1HasTree2(root1.left, root2.left) && doesTree1HasTree2(root1.right, root2.right);
    }
}
